package models

import (
	"fmt"
	"gitee.com/captials-team/ubdframe/src/common/utils"
	"gorm.io/gorm/schema"
)

const (
	ThirdWechat   = "wechat"   //微信小程序
	ThirdAlipay   = "alipay"   //支付宝小程序
	ThirdDouyin   = "douyin"   //抖音小程序
	ThirdBaidu    = "baidu"    //抖音小程序
	ThirdLark     = "lark"     //飞书小程序
	ThirdQQ       = "qq"       //qq
	ThirdKuaishou = "kuaishou" //快手小程序
	ThirdQuick    = "quick"    //快应用小程序
	Third360      = "360"      //360小程序
)

// UserThird 用于第三方平台登录
//
//	比如一个用户注册后可用手机号/邮箱/账户名登录，则在此表相应创建多条account记录
type UserThird struct {
	Id int64 `json:"id"  gorm:"primaryKey"`
	SqlTimeFields

	ThirdId      string `json:"third_id" gorm:"type:varchar(64);uniqueIndex"`       //第三方id
	ThirdUnionId string `json:"third_union_id" gorm:"type:varchar(64);uniqueIndex"` //第三方union id
	ThirdType    string `json:"third_type" gorm:"type:varchar(16)"`                 //第三方类型
	UId          int64  `json:"uid" gorm:"index"`

	Nickname   string `json:"nickname" gorm:"type:varchar(64)"`
	Avatar     string `json:"avatar" gorm:"type:varchar(256)"`
	ExtendInfo string `json:"extend_info,omitempty" gorm:"type:varchar(512)"` //拓展信息，可以存json

	//额外可获取的用户信息
	Phone string `json:"phone" gorm:"-"`
	Email string `json:"email" gorm:"-"`
}

func (r *UserThird) TableName(namer schema.Namer) string {
	return namer.TableName("user_third")
}

// ConvertUser 第三方转换为User
func (r *UserThird) ConvertUser() *User {
	if r.Nickname == "" {
		r.Nickname = fmt.Sprintf("用户-%s", utils.RandLetterFigureCode(8))
	}
	return &User{
		Id:            r.UId,
		SqlTimeFields: r.SqlTimeFields,
		Account:       r.ThirdType + "_" + utils.RandLetterFigureCode(8),
		Password:      utils.RandLetterFigureCode(32),
		Salt:          utils.RandLetterFigureCode(12),
		Nickname:      r.Nickname,
		Sex:           0,
		Avatar:        r.Avatar,
		Email:         r.Email,
		Phone:         r.Phone,
		LastLogin:     r.UpdatedAt,
		LastIp:        "",
		Status:        1,
	}
}
